package org.tang.data;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.tang.data.common.Constant;
import org.tang.data.common.Utils;

public class LiangBi {

	static int DAY_COUNT = 6;
	
	public static void main(String[] args) throws Exception {
		
		List<String> stocks = Utils.getStocks();
		List<LiangBiData> liangBiList = new ArrayList<LiangBi.LiangBiData>();
		
		for(int i=0;i<stocks.size();i++){
			LiangBiData obj = formLiangBiData(stocks.get(i),"2013-04-10");
			if(obj!=null){
				liangBiList.add(obj);
//				if(obj.stockCode.equals("sh601012"))
//					System.out.println(obj);
				//System.out.println("i = " + i + ",stockCode = "+obj.stockCode + ",liangbi = " + obj.liangBi);
				//System.out.println(obj);
			}
		}
		
		Collections.sort(liangBiList);
		
		//for(int i=liangBiList.size()-1 ;i>0;i--)
		for(int i=liangBiList.size()-1 ;i>liangBiList.size()-30;i--)
			System.out.println((liangBiList.size() - i)+"  "+liangBiList.get(i).stockCode + ", "+ liangBiList.get(i).liangBi +"," +liangBiList.get(i).upRate);
	}

	private static LiangBiData formLiangBiData(String stockCode, String date) {
		LiangBiData obj = null;
		try {
			List<String> KData = Utils.getLine(Constant.dirPre+Constant.dayKDir+stockCode.toUpperCase(Locale.CHINA)+".TXT", date,DAY_COUNT);
			if(KData.size()<DAY_COUNT)
				return null;
			String openLine = Utils.getOpenLine(Constant.dirPre+date+"\\" + stockCode+".txt");
			if(openLine == null)
				return null;
			obj = new LiangBiData();
			obj.stockCode = stockCode;
			obj.date = date;
			obj.KData = KData;
			obj.days = DAY_COUNT;
			obj.openLine = openLine;
			String openData[] = openLine.split("	");
			int vol = Integer.parseInt(openData[3]);
			double openPrice = Double.parseDouble(openData[1]);
			double lastClosePrice = 0;
			int total = 0;
			for(int i=1;i<KData.size();i++){
				String k = KData.get(i);
				String kDataLine[] = k.split("	");
				total += Integer.parseInt(kDataLine[5]);
				if(i==1)
					lastClosePrice= Double.parseDouble(kDataLine[4]);
			}
			obj.liangBi = vol*12.0/(total)*10000;
			obj.upRate = (openPrice-lastClosePrice)/lastClosePrice;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}



	static class LiangBiData  implements Comparable<LiangBiData>{
		String stockCode;
		List<String> KData;
		int days;
		String date;
		String  openLine;
		
		double upRate;
		double liangBi;
		public int compareTo(LiangBiData o) {
			if( this.liangBi== o.liangBi)
				return 0;
			else if( this.liangBi > o.liangBi)
				return 1;
			return -1;
		}
		@Override
		public String toString() {
			StringBuffer temp = new StringBuffer();
			for(int i=0;i<KData.size();i++)
				temp.append(KData.get(i)+",");
			temp.append(openLine);
			return temp.toString();
		}
	}

	

}
